package com.eling.elcms.travel.service.impl;

import com.eling.elcms.core.exception.BusinessException;
import com.eling.elcms.core.service.impl.GenericManagerImpl;
import com.eling.elcms.travel.dao.IProductBatchDao;
import com.eling.elcms.travel.model.ProductBatch;
import com.eling.elcms.travel.model.ProductRuleDetail;
import com.eling.elcms.travel.service.IProductBatchManager;
import com.eling.elcms.travel.service.IProductRuleDetailManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductBatchManagerImpl extends GenericManagerImpl<ProductBatch, Long> implements IProductBatchManager{

	IProductBatchDao batchDao;

	@Autowired
	private IProductRuleDetailManager detailManager;
	
	@Autowired
	public void setBatchDao(IProductBatchDao batchDao) {
		this.dao = batchDao;
		this.batchDao = batchDao;
	}

	@Override
	public ProductBatch changeseal(ProductBatch productBatch) {
		List<ProductBatch> list = this.query(productBatch);
		productBatch = list.get(0);
		Boolean seal = productBatch.getSeal();
		if(seal == true){
			seal = false;
		}else{
			seal = true;
		}
		productBatch.setSeal(seal);
		productBatch = this.save(productBatch);
		return productBatch;
	}

	@Override
	public ProductBatch savebatch(ProductBatch productBatch) {
		//名称唯一性判断
		ProductBatch all = new ProductBatch();
		List<ProductBatch> list = this.query(all);
		for (ProductBatch batch : list) {
			if(batch.getName().equals(productBatch.getName())){
				if(batch.getProduct().getPkProduct().equals(productBatch.getProduct().getPkProduct())){
					throw new BusinessException("同一产品批次名称不能重复!");
				}
			}
		}
		return this.save(productBatch);
	}

	@Override
	public boolean seekquote(Long pkProductBatch) {
		ProductRuleDetail detail = new ProductRuleDetail();
		List<ProductRuleDetail> list = detailManager.query(detail);
		boolean flag = false;
		for (ProductRuleDetail productDetail : list) {
			if(productDetail.getProductBatch() != null){
				if(productDetail.getProductBatch().getPkProductBatch().equals(pkProductBatch)){
					flag = true;
					break;
				}
			}
		}
		return flag;
	}
}
